package com.zhiyou100.test.test05.log4j;

import org.apache.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.Properties;

/**
 * @author yang
 * @className Demo01
 * @packageName com.zhiyou100.test.test05
 * @Description TODO log4j demo01 and 属性集文件的解析
 * @date 2020-08-12 09:22
 */
public class Demo01 {
    private static final Logger log = Logger.getLogger(Demo01.class);

    public static void main(String[] args) {
        log.debug("debug ......");
        log.error("error ....");


        // 属性集文件的解析
        final File file = new File("src/main/resources/db.properties");
        System.out.println(file.getAbsolutePath());
        try (
                // 1. 创建输入流和原文件关联
                final FileInputStream fileInputStream = new FileInputStream(file)
        ) {
            // 2 创建 Properties 类
            final Properties properties = new Properties();
            // 3 关联文件对应的输入流
            properties.load(fileInputStream);
            // 4 通过属性名获取值
            for (Map.Entry<Object, Object> objectObjectEntry : properties.entrySet()) {
                System.out.println(objectObjectEntry.getKey() + " : " + objectObjectEntry.getValue());
            }

            System.out.println("\n\n");
            System.out.println(properties.get("driver"));
            System.out.println(properties.get("url"));
            System.out.println(properties.get("username"));
            System.out.println(properties.get("password"));

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
